<template>
  <div>
    <!-- <el-input v-model="url" placeholder="请输入需要代理的地址"></el-input>
    <el-button type="primary" @click="start">确定</el-button>-->

  
   <!-- <el-button type="primary" @click="redcsv">确定</el-button> -->
    <div id="content">
      <trading-vue
        :data="chart"
        :width="this.width"
        :height="this.height"
        :color-back="colors.colorBack"
        :color-grid="colors.colorGrid"
        :color-text="colors.colorText"
      ></trading-vue>
    </div>
  </div>
</template>

<script>
import TradingVue from "trading-vue-js";
const express = require("express");
const proxy = require("http-proxy-middleware");
const app = express();
const fs = require("fs");
const path = require("path");
const xlsx2json = require("node-xlsx");
export default {
  name: "",
  components: { TradingVue },
  data() {
    return {
      url: "",
      num: 0,
      data: [],
      max: 0,
      min: 0,
      chart: {},
      width: window.innerWidth - 100,
      height: 650,
      colors: {
        colorBack: "#fff",
        colorGrid: "rgba(255,255,255,.1)",
        colorText: "#000"
      }
    };
  },
  mounted() {
    this.chart = {
      ohlcv: []
    };

    var content = document.querySelector("#content");

    content.ondragenter = content.ondragover = content.ondragleave = function() {
      return false; /*阻止默认行为*/
    };
    content.ondrop = e => {
      //阻止默认行为

      e.preventDefault();

      var path = e.dataTransfer.files[0].path;

      this.redcsv(path);
    };
  },
  methods: {
    start() {
      // console.log(this.url);
      app.use(
        "/",
        proxy({
          // 代理跨域目标接口
          target: this.url,
          changeOrigin: true,

          // 修改响应头信息，实现跨域并允许带cookie
          onProxyRes: function(proxyRes, req, res) {
            res.header("Access-Control-Allow-Origin", "*");
            res.header("Access-Control-Allow-Credentials", "true");
          },

          // 修改响应信息中的cookie域名
          cookieDomainRewrite: false // 可以为false，表示不修改
        })
      );

      app.listen(3000);
    },
    redcsv(url) {
      // let url = path.resolve("../stick/UHB_USDT_minute.csv");
      fs.readFile(url, (err, data) => {
        if (err) {
          console.log(err.stack);
          return;
        }
        var list = xlsx2json.parse(url);
        var data = list[0].data;
        console.log(data);
        for (let i = 0; i < data.length; i++) {
          data[i][0] = data[i][0] * 1000;
        }
        this.data = data;
        this.changeData();
      });
    },
    changeData() {
      if (this.data) {
        let kchart = {
          ohlcv: this.data
        };
        this.chart = kchart;
      }
    }
  }
};
</script>

<style lang="less" scoped>
</style>
